package com.endlessblue.visa.vo;

import java.util.Date;

import com.endlessblue.visa.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 业务系统返回结果
 * @author sonta
 *
 * @param <T>
 */
public class BusiResult<T> {
	
	// 返回结果代码
	private String code;
	
	// 返回结果信息
	private String msg;
	
	// 返回数据
	private T result;

	// 服务完成时间
    private Date completeTime;
    
    
    // 业务逻辑是否处理成功 （内部代码使用）
	private boolean isOk = true;
	
	// 业务逻辑返回附加信息（内部代码使用）
	private Object attach;
    
	public String getCode() {
		return code;
	}

	public BusiResult<T> setCode(String code) {
		this.code = code;
		return this;
	}
	
	public String getMsg() {
		return msg;
	}

	public BusiResult<T> setMsg(String msg) {
		this.msg = msg;
		return this;
	}
	
	public T getResult() {
		return result;
	}

	public BusiResult<T> setResult(T result) {
		this.result = result;
		return this;
	}
	
	/**
	 * 默认为异常
	 */
	public BusiResult() {
		this.code = BusiCode.ERROR_9999.getCode();
		this.msg = BusiCode.ERROR_9999.getMsg() ;
	}
	
	public BusiResult(String code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	
	

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("\r\n");
		sb.append("	code：").append(this.code).append("\r\n");
		sb.append("	msg：").append(this.msg).append("\r\n");
		sb.append("	completeTime：").append(this.completeTime).append("\r\n");
		sb.append("	result：").append(this.result == null ? null : JsonUtil.toJson(this.result));
		return sb.toString();
	}

	public Date getCompleteTime() {
		if (this.completeTime == null) {
			return new Date();
		}
		return completeTime;
	}

	public BusiResult<T> setCompleteTime(Date completeTime) {
		this.completeTime = completeTime;
		return this;
	}
	
	@JsonIgnore
	public boolean isOk() {
		return isOk;
	}

	public BusiResult<T> setOk(boolean isOk) {
		this.isOk = isOk;
		return this;
	}
	
	@JsonIgnore
	public Object getAttach() {
		return attach;
	}

	public BusiResult<T> setAttach(Object attach) {
		this.attach = attach;
		return this;
	}
	
	public BusiResult<T> setCoder(BusiCode code) {
		this.setCode(code.getCode());
		this.setMsg(code.getMsg());
		return this;
	}
	
}
